﻿using System;
using VegetableSale.NET.Utils;
using VegetableSale.NET.Entities;
using System.Data;

namespace VegetableSale.NET.Dao;

public class RtsaleMapper
{


    public List<Rtsale> getRtsaleList(QueryDTO queryDTO)
    {
        String sql = "";

        if (queryDTO.Keyword.Equals(""))
        {
            sql = "select * from rtsale;";
        }
        else
        {
            sql = "SELECT * FROM rtsale WHERE rtsmark='" + queryDTO.Keyword + "';";
        }


        List<Rtsale> rtsales = new List<Rtsale>();

        DataSet dateSet = MySqlHelper.GetDataSet(sql);
        DataTable dataTable = dateSet.Tables[0];

        foreach (DataRow row in dataTable.Rows)
        {
            uint id = (uint)row[0];
            int commid = (int)row[1];
            int rtsnumber = (int)row[2];
            int rtsmoney = (int)row[3];
            string rtsmark = (string)row[4];
            DateTime rtstime = (DateTime)row[5];

            Rtsale rtsale = new Rtsale(id, commid, rtsnumber, rtsmoney, rtsmark,rtstime);
            rtsales.Add(rtsale);
        }
        return rtsales;
    }

    public void addRtsale(RtsaleQuery rtsaleQuery)
    {
        string rtstime = TimeTransferUtils.GetSqlServerTimeString();
        int rtsmoney = rtsaleQuery.Rtsnumber * rtsaleQuery.Salemoney;
        string sql = "INSERT INTO rtsale (commid,rtsnumber,rtsmoney,rtsmark,rtstime) VALUES (" + rtsaleQuery.Commid +
                     "," + rtsaleQuery.Rtsnumber + "," + rtsmoney +
                     ",'" + rtsaleQuery.Rtsmark + "','" + rtstime + "');";

        MySqlHelper.ExecuteSql(sql);
    }
    
}

